//
//  VWPlayerController.swift
//  VideoPlayer_Example
//
//  Created by JY_NEW on 2021/10/14.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import ijkPlayerManager
import UIKit

class VWPlayerController: VideoPlayerViewController {
    
    weak var delegate: VWPlayerControlDelegate? {
        didSet {
            self.player.layerManager.layers.forEach({ ($0 as? VWPlayerLayerType)?.delegate = self.delegate })
        }
    }
    let context = VWPlayerControlContext()
    
//    private let splashPauseFlag = makePauseFlag() //<-- 给开屏广告用的暂停标识
    private let loginQuitFullScreenFlag = makeQuitFullScreenFlag()
    
    private let loadingState = VWPlayerLoadingStateLayer()
    private let networkState = VWPlayerNetworkStateLayer()
    private let errorState = VWPlayerErrorStateLayer()
    private let finishedState = VWPlayerFinishedStateLayer()
    
    private let basicControl = VWPlayerBasicControlLayer()
    private let swipeControl = VWPlayerSwipeControlLayer()
    private let longPressControl = VWPlayerLongPressControlLayer()
    private lazy var speedControl: VWPlayerSpeedControlLayer = self.createControl()
    
    private let noDataBlock = VWPlayerNoDataBlockLayer()
    private let allow5gBlock = VWPlayer5GBlockLayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
#if canImport(IJKMediaFrameworkWithSSL)
        self.player.ijkOptions.setPlayerOptionIntValue(5, forKey: "framedrop")
        self.player.ijkOptions.setPlayerOptionIntValue(1024 * 1024 * 5, forKey: "max-buffer-size")
#endif
        
        self.player.observer.fullScreen.addTarget(self, action: #selector(self.onFullScreen))
        
        self.buildLayers()
    }
    @objc func onFullScreen() {
        self.delegate?.layoutPlayer(self.view, isFullScreen: self.player.isFullScreen)
    }
    deinit {
        self.player.observer.fullScreen.removeTarget(self)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.returnFullScreen(for: self.loginQuitFullScreenFlag)
    }
    
    func buildLayers() {
        self.buildPluginLayers()
        self.buildStateLayers()
        self.buildControlLayers()
        self.buildBlockLayers()
    }
    
    func buildPluginLayers() {
        
    }
    
    func buildStateLayers() {
        self.addLayer(loadingState)
        self.addLayer(networkState)
        self.addLayer(errorState)
        self.addLayer(finishedState)
    }
    
    func buildControlLayers() {
        basicControl.speedAction = { [weak self] in
            self?.speedControl.active()
        }
        self.addLayer(basicControl)
        self.addLayer(swipeControl)
        self.addLayer(longPressControl)
    }
    
    func buildBlockLayers() {
        self.addLayer(allow5gBlock)
        self.addLayer(noDataBlock)
    }
    func addLayer(_ layer: VWPlayerLayerType) {
        layer.delegate = self.delegate
        layer.context = self.context
        self.player.layerManager.addLayer(layer)
    }
    func createControl<LayerType: VWPlayerSecondBaseControlLayer>() -> LayerType {
        let layer = LayerType()
        self.addLayer(layer)
        return layer
    }
    func playNoData() {
        self.noDataBlock.active()
    }
}
